<?php
class Router {

	private $controller;
	private $action;
	private $params;

	public function parseUrl($url) {
		$uri = $this->_removeBase($url);
		$urlArray = array();
		$urlArray = explode('/',$uri);
		array_shift($urlArray);
		!isset($urlArray[0]) ? $this->controller = null :
		$this->controller = 'Controller\\'.$this->_controllerConv($urlArray[0]);
		array_shift($urlArray);
		!isset($urlArray[0]) ? $this->action = null :
		$this->action = $urlArray[0];
		array_shift($urlArray);
		!isset($urlArray[0]) ? $this->params = null :
		$this->params = array_map(function($param) {
			return urldecode($param);
		}, $urlArray);
	}

	public function checkPermission() {
		if(!isset($_SESSION["login"])) {
			$this->controller='Controller\\Usuarios';
			$this->action="login";
		} 
	}
	
	public function getController() {
		return $this->controller;
	}

	public function getAction() {
		return $this->action;
	}

	public function getParams() {
		return $this->params;
	}

	private function _removeBase($url) {
		return str_replace(WEBROOT,'',$url);
	}

	private function _controllerConv($controller) {
		return ucfirst(strtolower($controller));
	}
}